import json

import socks
from telethon import TelegramClient
import os

from telethon.tl.types import MessageMediaDocument

# 填写您的 API ID 和 API Hash
api_id = '22762141'
api_hash = '7e8be8f925116c3f401c6f272410ad15'
phone_number = '+254731299598'
chance='dhfhfbf464'
download_folder = f'E:\\work\\{chance}\\{chance}'
os.makedirs(download_folder, exist_ok=True)
proxy = (socks.SOCKS5, '127.0.0.1', 10808, True)

# 定义自定义的 TelegramClient，设置 User-Agent

async def main():
    async with TelegramClient('session_name', api_id, api_hash , proxy=proxy) as client:
        async for message in client.iter_messages(chance):
            print(message)
            try:
                if message.media:  # 检查是否有媒体
                    photo = await client.download_media(message.media, file=download_folder)
                    print(f'Downloaded: {photo}')

                else:
                    print(f'Message: {message.text}')
            except:
                pass


if __name__ == '__main__':
    import asyncio
    asyncio.run(main())
